package wangm.repository;

import wangm.bean.Pagination;
import wangm.bean.User;
import wangm.util.DBOperatorComponent;
import wangm.util.RandomUtil;
import wangm.util.SystemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author zk
 * @date 2018/2/26
 */
@Repository
public class UserRepository {
    private static Logger logger = LoggerFactory.getLogger(ExaminationTopicRepository.class);
    /**
     * this table Name(same as PaperRepository)
     */
    public static final String thisTableName = "T_USER";
    /**
     * db component
     */
    @Autowired
    private DBOperatorComponent dbOperatorComponent;

    public User getUserById(String id){
        String sql = "select * from " + thisTableName + " where id = ?";
        try {
            return dbOperatorComponent.getById(id,thisTableName,User.class);
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
        return null;
    }
    /**
     * 根据学号和密码获取用户
     *
     * @param number number
     * @param password password
     * @return userBean
     */
    public User getUserByNumberAndPassword(String number, String password){
        String sql = "select * from " + thisTableName + " where number = ? and password = ?";
        try {
            List<User> users = dbOperatorComponent.getBeanList(User.class,sql,number,password);
            return CollectionUtils.isEmpty(users) ? null : users.get(0);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 根据用户名获取用户
     *
     * @param username userName
     * @return User
     */
    public User getUserByUserName(String username){
        String sql = "select * from " + thisTableName + " where username = ?";
        try {
            List<User> users = dbOperatorComponent.getBeanList(User.class,sql,username);
            return CollectionUtils.isEmpty(users) ? null : users.get(0);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 根据学号获取用户
     *
     * @param number
     * @return User
     */
    public User getUserByNumber(String number){
        String sql = "select * from " + thisTableName + " where number = ?";
        try {
            List<User> users = dbOperatorComponent.getBeanList(User.class,sql,number);
            return CollectionUtils.isEmpty(users) ? null : users.get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加人员
     *
     * @param user user
     */
    public void insertUser(User user){
        String sql = "insert into " + thisTableName + " (id,username,password,discipline,number,admin) values(?,?,?,?,?,?)";
        try {
            dbOperatorComponent.update(sql,RandomUtil.getUUID(16),user.getUsername(),SystemUtil.enSHA256(user.getPassword()),user.getDiscipline(),user.getNumber(),user.getAdmin());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 更新人员(除主键外，其他所有字段都更新)
     *
     * @param user user
     */
    public void updateUser(User user){
        String sql = "update " + thisTableName + " set username = ? and password = ? where id = ?";
        try {
            dbOperatorComponent.update(sql,user.getId(),user.getUsername(),user.getPassword());
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 获取用户列表
     *
     * @param pagination 分页
     * @param admin admin 1 是管理员 0 不是管理员
     * @return List<user></user>
     */
    public List<User> getUserList(Integer admin, Pagination<User> pagination){
        String sql = "select * from " + thisTableName + " where admin = ?";
        try {
            return dbOperatorComponent.getBeanList(User.class,sql,pagination,String.valueOf(admin));
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
        return null;
    }

    /**
     * 更新指定user
     *
     * @param user user
     */
    public void modifyUser(User user){
        List paramers = new ArrayList();

        StringBuffer sql = new StringBuffer("update " + thisTableName + " set id = ?");
        paramers.add(user.getId());
        if(!StringUtils.isEmpty(user.getUsername())){
            sql.append(",username = ?");
            paramers.add(user.getUsername());
        }

        if(!StringUtils.isEmpty(user.getNumber())){
            sql.append(",number = ?");
            paramers.add(user.getNumber());
        }

        if(!StringUtils.isEmpty(user.getAdmin())){
            sql.append(",admin = ?");
            paramers.add(user.getAdmin());
        }

        if(!StringUtils.isEmpty(user.getDiscipline())){
            sql.append(",discipline = ?");
            paramers.add(user.getDiscipline());
        }
        //添加条件
        sql.append(" where id = ? ");
        paramers.add(user.getId());
        try {
            dbOperatorComponent.update(sql.toString(),paramers.toArray());
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 移除用户 id
     *
     * @param id 用户id
     */
    public void remvoeUser(String id){
        String sql = "delete from " + thisTableName + " where id = ?";
        try {
            dbOperatorComponent.update(sql,id);
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
    }
}
